'use strict';
var gulp = require('gulp'),
    connect = require('gulp-connect'),
    less = require('gulp-less'),
    rjs = require('gulp-requirejs'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    minifyCSS = require('gulp-minify-css'),
    path = require('path'); //require优化

var shell = require('gulp-shell');


// 处理的page lists
var PAGES = ['order', 'order-error','order-monitor', 'order-provider', 'order/preorder-monitor', 'order/list', 'order/pintuan', 'order/prebuy','order/nextday',
    'shop/city', 'shop/config', 'shop/dispatch', 'shop/self','shop/deliverfee','shop/warehouse',
    'bank/shop','bank/disburse','bank/frezz','bank/check','bank/template',
    'user/gencode', 'user/search', 'user/detail', 'user/blackuser', 'user/token', 'user/hongbao','user/syncOrgData',
    'home', 'msgcenter/sms', 'msgcenter/push','msgcenter/pushFailedList','msgcenter/shopJoin',
    'other/rebuy', 'other/weixin', 'other/newuser', 'other/dataright', 'other/feedback','other/rate',
    'tracker/channel',
    'outflat/outflatGoods','outflat/addGoods','outflat/outshopmanager','outflat/modifyPrice',
    'pos/upgrade','pos/adv','pos/register',
    'outflat/category','outflat/categoryGoods',
    'print/right','print/psrsonal','print/wayBillQuery',
    'item/subcat',
    'order/taobao-order-monitor'
    ];
//var CURRENT_PAGE = 'cart';
// server 端口号
var PORT = 8080;


gulp.task('connect', function () {
    connect.server({
        root: ['./'],
        port: PORT,
        liveReload: true
    })
})


gulp.task('less-dev', function () {

    ['base'].forEach(function (v) {
        gulp.src('src/c/' + v + '/index.less')
            .pipe(less())
            .pipe(gulp.dest('src/c/' + v))
            .pipe(minifyCSS())
            .pipe(gulp.dest('./build/c/' + v));
    });


    PAGES.forEach(function (v) {
        gulp.src('src/p/' + v + '/index.less')
            .pipe(less())
            .pipe(minifyCSS())
            .pipe(gulp.dest('src/p/' + v));
    });
});

gulp.task('less-build', function () {

    ['base'].forEach(function (v) {
        gulp.src('src/c/' + v + '/index.less')
            .pipe(less())
            .pipe(gulp.dest('src/c/' + v))
            .pipe(minifyCSS())
            .pipe(gulp.dest('./build/c/' + v));
    });

    PAGES.forEach(function (v) {
        gulp.src('src/p/' + v + '/index.less')
            .pipe(less())
            .pipe(gulp.dest('src/p/' + v))
            .pipe(minifyCSS())
            .pipe(gulp.dest('./build/p/' + v));
    });
});


gulp.task('js-build', function () {


    PAGES.forEach(function (v) {
        rjs({
            baseUrl: 'src',
            name: 'p/' + v + '/index',
            out: 'p/' + v + '/index.js',
            removeCombined: true,
            paths: {
                // 带上http的 避免打包libjs
                zepto: 'http://zepto.js',
                underscore: 'htp://underscore.js',
                juicer: 'htp://underscore.js'
            },
            shim: {
                underscore: {
                    exports: '_'
                },
                zepto: {
                    exports: '$'
                }
            }
        })
            .pipe(uglify())
            .pipe(gulp.dest('./build/'));
    });
});


gulp.task('juicer-build', shell.task([
    'juicer ./src/p/outflat/category/actCategory-list.juicer.html -af ./src/p/outflat/category/actCategory-list-tpl.js',
    'juicer ./src/p/outflat/categoryGoods/categoryGoods-list.juicer.html -af ./src/p/outflat/categoryGoods/categoryGoods-list-tpl.js',
    'juicer ./src/p/outflat/outflatGoods/outflatMarks-list.juicer.html -af ./src/p/outflat/outflatGoods/outflatMarks-list-tpl.js',
    'juicer ./src/p/outflat/outflatGoods/onfailnotice-list.juicer.html -af ./src/p/outflat/outflatGoods/onfailnotice-list-tpl.js',
    'juicer ./src/p/outflat/outflatGoods/outflatGoods-list.juicer.html -af ./src/p/outflat/outflatGoods/outflatGoods-list-tpl.js',
    'juicer ./src/c/order-detail/order-detail.juicer.html -af ./src/c/order-detail/order-detail-tpl.js',
    'juicer ./src/c/preorder-resend/repreorder-detail.juicer.html -af ./src/c/preorder-resend/repreorder-detail-tpl.js',
    'juicer ./src/c/modify-address/modify-address.juicer.html -af ./src/c/modify-address/modify-address-tpl.js',
    'juicer ./src/c/hongbao/hongbao-detail.juicer.html -af ./src/c/hongbao/hongbao-detail-tpl.js',
    'juicer ./src/p/order/mods/list.juicer.html -af ./src/p/order/mods/list-tpl.js',
    'juicer ./src/p/order-monitor/mods/list.juicer.html -af ./src/p/order-monitor/mods/list-tpl.js',
    'juicer ./src/p/order-monitor/mods/tab.juicer.html -af ./src/p/order-monitor/mods/tab-tpl.js',
    'juicer ./src/c/nav/nav.juicer.html -af ./src/c/nav/nav-tpl.js',
    'juicer ./src/c/nav/nav-top.juicer.html -af ./src/c/nav/nav-top-tpl.js',
    'juicer ./src/p/shop/config/mods/white-user.juicer.html -af ./src/p/shop/config/mods/white-user-tpl.js',
    'juicer ./src/p/shop/config/mods/user-list.juicer.html -af ./src/p/shop/config/mods/user-list-tpl.js',
    'juicer ./src/p/other/feedback/feedback-list.juicer.html -af ./src/p/other/feedback/feedback-list-tpl.js',
    'juicer ./src/c/refund/refund.juicer.html -af ./src/c/refund/refund-tpl.js',
    'juicer ./src/c/logistic/logistic.juicer.html -af ./src/c/logistic/logistic-tpl.js',
    'juicer ./src/c/preorder-resend/resend-form.juicer.html -af ./src/c/preorder-resend/resend-form-tpl.js',
    'juicer ./src/c/template/template.juicer.html -af ./src/c/template/template-tpl.js',
    'juicer ./src/c/template/rule.juicer.html -af ./src/c/template/rule-tpl.js',
    'juicer ./src/p/order-error/mods/list.juicer.html -af ./src/p/order-error/mods/list-tpl.js',
    'juicer ./src/p/outflat/outshopmanager/outflat-list.juicer.html -af ./src/p/outflat/outshopmanager/outflat-list-tpl.js',
    'juicer ./src/p/order/taobao-order-monitor/taobaoOrderMonitor.juicer.html -af ./src/p/order/taobao-order-monitor/taobaoOrderMonitor-tpl.js'

]));


gulp.task('watch', function () {
    // gulp.watch(['src/**/*.html','src/**/*.js','src/**/*.less','src/**/*.css'], ['html']);
    gulp.watch(['src/**/*.less'], ['less-dev', 'less-build']);
    // gulp.watch(['src/**/*.js'], ['js-build']);
    gulp.watch(['src/**/*.juicer.html'], ['juicer-build']); // /Users/bf/Documents/f/buyer-trade/
    gulp.watch(['juicer-build']);
});


gulp.task('dev', ['connect', 'watch']);
gulp.task('build', ['less-build', 'js-build']);
gulp.task('default', ['less-build', 'js-build', 'juicer-build']);

